package com.zline.foodraise.wx;

import android.content.Context;

import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.entity.AppPref_;
import com.zline.foodraise.entity.WxPayInfo;
import com.zline.foodraise.http.HttpDataParsing;
import com.zline.foodraise.util.LUtil;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by heshantao on 15/8/12.
 * 微信支付
 */
@EBean
public class WxPayUtil {
    private static final String LOG_TAG=WxPayUtil.class.getSimpleName();
    @RootContext
    Context cxt;
    IWXAPI iwxapi;
    PayReq req;
    @Bean
    HttpDataParsing httpDataParsing;
    @Pref
    AppPref_ appPref;

    void  regToWx(){
        iwxapi = WXAPIFactory.createWXAPI(cxt, null);
        iwxapi.registerApp(Constant.WX_APP_ID);
    }

    //生成支付参数
   void genPayReq(WxPayInfo payInfo) {
        req=new PayReq();
        req.appId =payInfo.getAppid();
        req.partnerId = payInfo.getPartnerid();
        req.prepayId = payInfo.getPrepayid();
        req.packageValue = payInfo.getmPackage();
        req.nonceStr =payInfo.getNoncestr();
        req.timeStamp=payInfo.getTimestamp();
        req.sign = payInfo.getSign();
    }

    //支付
    public void pay(JSONObject jsonObject){
        regToWx();
        WxPayInfo wxPayInfo= httpDataParsing.getWxPayInfo(jsonObject);
        genPayReq(wxPayInfo);
        saveSomePayInfo(jsonObject);
        LUtil.i(LOG_TAG,"_________开始通信________"+jsonObject);
        iwxapi.sendReq(req);
    }

    //存储支付的一些信息
    void saveSomePayInfo(JSONObject jsonObject){
        try {
            if (jsonObject.opt(Constant.ONLINE_PAY_METHOD) != null &&
                    !jsonObject.isNull(Constant.ONLINE_PAY_METHOD) &&
                    jsonObject.get(Constant.ONLINE_PAY_METHOD) != JSONObject.NULL) {
                int payMethod = jsonObject.getInt(Constant.ONLINE_PAY_METHOD);
                appPref.edit().onlinePayMethod().put(payMethod).apply();
            }

            if (jsonObject.opt(Constant.PROJECT_ID) != null &&
                    !jsonObject.isNull(Constant.PROJECT_ID) &&
                    jsonObject.get(Constant.PROJECT_ID) != JSONObject.NULL) {
                String id=jsonObject.getString(Constant.PROJECT_ID);
                long projectId = Long.parseLong(id);
                appPref.edit().projectId().put(projectId).apply();
            }


            if (jsonObject.opt(Constant.ORDER_CODE) != null &&
                    !jsonObject.isNull(Constant.ORDER_CODE) &&
                    jsonObject.get(Constant.ORDER_CODE) != JSONObject.NULL) {
                String orderCode=jsonObject.getString(Constant.ORDER_CODE);
                appPref.edit().orderCode().put(orderCode).apply();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}
